package com.lagou.util;

import org.apache.dubbo.rpc.RpcContext;

import javax.servlet.ServletRequest;
import java.util.Map;

public class DubboTraffic {
    private static final String REQUEST_IP = "request_ip";

    public static void saveFrom(ServletRequest servletRequest) {
        if (servletRequest == null) {
            return;
        }
        final Map<String, String> attachments = RpcContext.getContext().getAttachments();
        String ip = servletRequest.getRemoteAddr();
        attachments.put(REQUEST_IP, ip);
    }

    public static String requestIp() {
        final Map<String, String> attachments = RpcContext.getContext().getAttachments();
        return attachments.get(REQUEST_IP);
    }

}
